package com.kylin;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelWriteTest {

    String path = "C:\\Data\\IntelliJ IDEA Work Space\\kylin-poi\\";

    @Test
    public void testWrite03() throws IOException {
        //1.创建一个工作簿
        Workbook workbook = new HSSFWorkbook();//03
        //2.创建一个工作表
        Sheet sheet = workbook.createSheet("Kylin观众统计表03");
        //3.创建一个行（1，1）
        Row row1 = sheet.createRow(0);
        //创建一个单元格
        Cell cell11 = row1.createCell(0);//（1,1)
        cell11.setCellValue("今日新增观众");

        Cell cell12 = row1.createCell(1);//(1,2)
        cell12.setCellValue(666);

        //第二行
        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);//（2,1)
        cell21.setCellValue("统计时间");

        Cell cell22 = row2.createCell(1);//（2,2)
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);

        //生成一张表
        FileOutputStream fileOutputStream = new FileOutputStream(path + "Kylin观众统计表03.xls");
        workbook.write(fileOutputStream);

        //关闭流
        fileOutputStream.close();
        System.out.println("Kylin观众统计表03生成完毕");
    }


    @Test
    public void testWrite07() throws IOException {
        //1.创建一个工作簿
        Workbook workbook = new XSSFWorkbook();//07
        //2.创建一个工作表
        Sheet sheet = workbook.createSheet("Kylin观众统计表07");
        //3.创建一个行（1，1）
        Row row1 = sheet.createRow(0);
        //创建一个单元格
        Cell cell11 = row1.createCell(0);//（1,1)
        cell11.setCellValue("今日新增观众");

        Cell cell12 = row1.createCell(1);//(1,2)
        cell12.setCellValue(666);

        //第二行
        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);//（2,1)
        cell21.setCellValue("统计时间");

        Cell cell22 = row2.createCell(1);//（2,2)
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);

        //生成一张表 xlsx结尾
        FileOutputStream fileOutputStream = new FileOutputStream(path + "Kylin观众统计表07.xlsx");
        workbook.write(fileOutputStream);

        //关闭流
        fileOutputStream.close();
        System.out.println("Kylin观众统计表03生成完毕");
    }

    @Test
    public void testWrite03BigData() throws IOException {
        //时间
        long begin = System.currentTimeMillis();

        //创建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        //创建一张表
        Sheet sheet = workbook.createSheet();

        //行
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            //列
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(rowNum + "," + cellNum);
            }
        }
        System.out.println("over");
        long end = System.currentTimeMillis();

        FileOutputStream fileOutputStream = new FileOutputStream(path + "testWrite03BigData.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("03总耗时：" + ((double) end - begin / 1000));
    }

    @Test
    public void testWrite07BigData() throws IOException {
        //时间
        long begin = System.currentTimeMillis();

        //创建一个工作簿
        Workbook workbook = new XSSFWorkbook();
        //创建一张表
        Sheet sheet = workbook.createSheet();

        //行
        for (int rowNum = 0; rowNum < 100000; rowNum++) {
            Row row = sheet.createRow(rowNum);
            //列
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(rowNum + "," + cellNum);
            }
        }
        System.out.println("over");
        long end = System.currentTimeMillis();

        FileOutputStream fileOutputStream = new FileOutputStream(path + "testWrite07BigData.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("07总耗时：" + ((double) (end - begin) / 1000) + "秒");
    }

    @Test
    public void testWrite07BigDataS() throws IOException {
        //时间
        long begin = System.currentTimeMillis();
        //创建一个工作簿
        Workbook workbook = new SXSSFWorkbook();
        //创建一张表
        Sheet sheet = workbook.createSheet();

        //行
        for (int rowNum = 0; rowNum < 100000; rowNum++) {
            Row row = sheet.createRow(rowNum);
            //列
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(rowNum + "," + cellNum);
            }
        }
        System.out.println("over");
        long end = System.currentTimeMillis();

        FileOutputStream fileOutputStream = new FileOutputStream(path + "testWrite07BigDataS.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        ((SXSSFWorkbook) workbook).dispose();
        System.out.println("07S总耗时：" + ((double) (end - begin) / 1000) + "秒");
    }
}
